package com.fr.lintcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

/**
*作者：furong
*日期：2016年12月27日
*时间：下午5:49:52
*/
public class Q20 {

    @Test
    public void sieve() {
        int n = 15;
        List<Map.Entry<Integer, Double>> list = dicesSum(n);
        for (Map.Entry<Integer, Double> e : list) {
            System.out.println(e.getKey() + ":" + e.getValue());
        }
    }

    public List<Map.Entry<Integer, Double>> dicesSum(int n) {
        List<Map.Entry<Integer, Double>> rs = new ArrayList<>();
        double total = Math.pow(6, n);
        long[][] count = new long[n][6 * n + 1];
        count[0][1] = 1;
        count[0][2] = 1;
        count[0][3] = 1;
        count[0][4] = 1;
        count[0][5] = 1;
        count[0][6] = 1;
        for (int i = 1; i < n; i++) {
            for (int j = i + 1; j <= 6 * n; j++) {
                for (int k = 1; k <= 6 && k < j; k++) {
                    count[i][j] += count[i - 1][j - k];
                }
            }
        }
        Map<Integer, Double> map = new HashMap<>();
        for (int j = n; j <= 6 * n; j++) {
            map.put(j, count[n - 1][j] / total);
        }
        for (Map.Entry<Integer, Double> e : map.entrySet()) {
            rs.add(e);
        }
        Collections.sort(rs, new c());
        return rs;
    }

    class c implements Comparator<Map.Entry<Integer, Double>> {

        @Override
        public int compare(Map.Entry<Integer, Double> o1, Map.Entry<Integer, Double> o2) {
            return o1.getKey() - o2.getKey();
        }

    }
}
